/**
 * 
 */
package com.ginger.rabbitmq.exchange.api.message;

import com.ginger.properties.RabbitMQProperties;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 * @Description: 生产者
 * @author 姜锋
 * @date 2019年3月29日 上午9:40:05 
 * @version V1.0   
 *
 */
public class Producer {
	
	public static void main(String[] args) throws Exception{
		// 1.创建连接工厂
		ConnectionFactory connectionFactory = new ConnectionFactory();
		
		//1.1设置连接信息
		connectionFactory.setHost(RabbitMQProperties.RABBITMQ_HOST);
		connectionFactory.setPort(RabbitMQProperties.RABBITMQ_PORT);
		connectionFactory.setVirtualHost(RabbitMQProperties.RABBITMQ_VIRTUAL_HOST);
		connectionFactory.setUsername(RabbitMQProperties.USER_NAME);
		connectionFactory.setPassword(RabbitMQProperties.PASSWORD);
		
		// 2.通过连接工厂创建连接
		Connection connection = connectionFactory.newConnection();
		// 3.通过connection创建
		Channel channel = connection.createChannel();
		
		//4.通过channel发送数据
		
		for (int i = 0; i < 5; i++) {
			String message =  "这是 第"+i+"条生产的数据:  Hello RabbitMQ!";
			
			//channel.basicPublish(交换机名称, RouttingKey, null, 内容);
			channel.basicPublish("", "testGinger001", null, message.getBytes());
				
		}
		
		//5 关闭连接
		channel.close();
		connection.close();
		
	}
}
